<?php
/**
 * 【巡检功能】接口控制器
 * 
 */
class inspectionAction extends apiAction {
	
	/**
	 * 获得巡检表列表
	 */
	public function index(){
		$page	= I('page', 1);	// 要显示第几页数据
		$num	= I('num', 20);	// 每页显示多少条数据
		$month	= I('month', date('Y-m')); //月份
		
		$map['_string'] = "DATE_FORMAT(date, '%Y-%m') = '{$month}'";
		//如果不是超级管理员，则只能查看自己的
		if($this->user['roleId'] != 1){
			$map['userId'] = $this->user['id'];
		}
		
		$data	= D(MODULE_NAME)->field("i.id,i.date,s.name AS storeName")->alias('i')
				->join(C('DB_PREFIX')."store AS s ON s.id = i.storeId")
				->where($map)->limit($num)->page($page)->order("date")->select();
		
		if($data){
			$this->ajaxReturn(200, L('success'), $data);
		}else{
			$this->ajaxReturn(300,  L('no_more'));
		}
	}
	
	/**
	 * 获得巡检表部分的填写表单（主表，针对厅店）
	 */
	public function table(){
		$storeId = I('storeId');
		$store = D('store')->where(array('id'=>$storeId))->find();
		$inspection = D('inspection')->where(array('date'=>date('Y-m-d'), 'storeId'=>$storeId))->find();
		if($inspection){
			$inspectionId = $inspection['id'];
		}else{
			$inspectionId = 0;
		}
		$this->assign('table',D(MODULE_NAME)->getFormField('inspection', $inspectionId));
		$this->assign('store', $store);
		$this->display();
	}
	
	/**
	 * 提交巡检表数据
	 */
	public function addTable(){
		$storeId = I('storeId');
		$value = I('v');
		
		if($storeId && $value){
			$inspection = D('inspection')->where(array('date'=>date('Y-m-d'), 'storeId'=>$storeId))->find();
			//如果找不到这条巡检任务，则自动创建一条，非任务型巡检记录
			if(!$inspection){
				$inspection['id'] = D('inspection')->add(array('date'=>date('Y-m-d'), 'storeId'=>$storeId));
			}
			//记录表单会中值
			foreach ($value as $key=>$val){
				$data['pid'] = $inspection['id'];
				$data['optionid'] = $key;
				$data['modular'] = 'inspection';
				
				$attributevar = D('attributevar')->where($data)->find();
				if($attributevar){
					D('attributevar')->where($data)->save(array('value'=>$val));
				}else{
					$data['value'] = $val;
					D('attributevar')->add($data);
				}
			}
			//更新巡检表状态
			D('inspection')->where(array('date'=>date('Y-m-d'), 'storeId'=>$storeId))->save(array('userId'=>$this->user['id'], 'createTime'=>time()));
			
			$this->redirect(U('api/inspection/detail?id='.$inspection['id']));
		}else{
			$this->display("public:error");
		}
	}

	/**
	 * 查看巡检记录详情
	 */
	public function detail(){
		$id = I('id');
		$res = D('inspection')->where(array('id'=>$id))->find();
		if($res){
			$this->assign('table',D(MODULE_NAME)->getFormField('inspection', $id));	//巡检表内容
			$this->assign('store', D('store')->where(array('id'=>$res['storeId']))->find());	//厅店信息
			
			$this->display();
		}
	}
	
	/**
	 * 提交巡检图片
	 */
	public function addImages(){
		$storeId = I('storeId');
		$gps = I('gps');	//定位信息
		$inspection = D('inspection')->where(array('date'=>date('Y-m-d'), 'storeId'=>$storeId, 'userId'=>$this->user['id']))->find();
		if(! $inspection){
			$this->ajaxReturn(300, L('please_submit_inspection_table'));
		}
		
		if(! $gps){
			$this->ajaxReturn(300, L('gps_is_require'));
		}
		
		$images = $this->saveAttachment('images');
		if($images['error']){
			$this->ajaxReturn(300, $images['error']);
		}
		
		if(D('inspection')->where(array('id'=>$inspection['id']))->save(array('images'=>$images['data'],'gps'=>$gps))){
			$this->ajaxReturn(200, L('success') );
		}else{
			$this->ajaxReturn(300, $images['error']);
		}
	}
	
}
/* End of file inspectionAction.class.php */